//
//  HLGiftCollectionLayout.m
//  YueYue
//
//  Created by hulu001 on 2023/6/2.
//

#import "HLGiftCollectionLayout.h"

@interface HLGiftCollectionLayout ()

@end

@implementation HLGiftCollectionLayout
/**
    这里只简单的实现只支持1个section，而且section的items个数必须是 itemCountPerRow * rowCount 的整数倍。这里需要在UICollectionView的代理里面做好数组越界检查，防止数组越界造成崩溃。
 */


- (void)prepareLayout {
    [super prepareLayout];
    
    self.allAttributes = [NSMutableArray arrayWithCapacity:0];
    // iCount section==0 的cell个数
    NSInteger iCount = [self.collectionView numberOfItemsInSection:0];
    for (NSInteger i = 0; i < iCount; i++) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
        UICollectionViewLayoutAttributes *attributes = [self layoutAttributesForItemAtIndexPath:indexPath];
        [self.allAttributes addObject:attributes];
    }
}

- (CGSize)collectionViewContentSize {
    
    if (self.allAttributes.count == 0) {
        return [super collectionViewContentSize];
    }
    
    CGFloat height = [super collectionViewContentSize].height;
    
    NSInteger countInPage = self.rowCount*self.itemCountPerRow;
    
    NSInteger numOfPage = ((self.allAttributes.count-1) / countInPage) + 1;
    
    CGFloat pageWidth = self.collectionView.width; //self.sectionInset.left + self.sectionInset.right + (self.itemCountPerRow - 1) * self.minimumLineSpacing + (self.itemSize.width * self.itemCountPerRow);
    
    NSInteger pageCount ;
    if (self.allAttributes.count % countInPage == 0) {
        pageCount = self.allAttributes.count / countInPage;
    } else {
        pageCount = self.allAttributes.count / countInPage + 1;
    }
    
    NSLog(@"[ppgift] ViewContentWidth = %f, pageWidth", pageWidth * pageCount);
    return CGSizeMake(pageWidth * pageCount, height);
}

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
    NSUInteger item = indexPath.item; // 第几个item
    NSUInteger x; // 该item的 x坐标
    NSUInteger y; // 该item的 y坐标
    
    [self targetPositionWithItem:item resultX:&x resultY:&y];
    
    NSUInteger item2 = [self originItemAtX:x y:y];
    
    NSIndexPath *newIndexPath = [NSIndexPath indexPathForItem:item2 inSection:indexPath.section];
    UICollectionViewLayoutAttributes *newAttributes = [super layoutAttributesForItemAtIndexPath:newIndexPath];
    newAttributes.indexPath = indexPath; // 本应该是第item个cell，计算后应该显示在newIndexPath这个位置，将newIndexPath作为item的位置返回出去
    newAttributes.frame = [self rectAtItem:item]; // 计算新位置的frame
    
    return newAttributes;
    
}

- (NSArray<__kindof UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
    
//    NSArray *attributes = [super layoutAttributesForElementsInRect:rect];
//
//    NSMutableArray *tmps = [NSMutableArray arrayWithCapacity:0];
//
//    for (UICollectionViewLayoutAttributes *bute1 in attributes) {
//        for (UICollectionViewLayoutAttributes *bute2 in self.allAttributes) {
//            if (bute1.indexPath.item == bute2.indexPath.item) {
//                [tmps addObject:bute2];
//                break;
//            }
//        }
//    }
//
//    return tmps;
    
    NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(UICollectionViewLayoutAttributes *attributes, NSDictionary *bindings) {
            return CGRectIntersectsRect(rect, attributes.frame);
        }];

    NSArray *visibleAttributes = [self.allAttributes filteredArrayUsingPredicate:predicate];
    return visibleAttributes;
}

// 根据 item 计算目标item的位置 // (第几个item)
// x 横向偏移  y 竖向偏移
- (void)targetPositionWithItem:(NSUInteger)item
                       resultX:(NSUInteger *)x
                       resultY:(NSUInteger *)y {
    
    NSUInteger page = item/(self.itemCountPerRow * self.rowCount); // 第几页(左右翻页)： 每行的cell个数 * 几行
    
    NSUInteger itemX = item % self.itemCountPerRow + page * self.itemCountPerRow; //
    NSUInteger itemY = item / self.itemCountPerRow - page * self.rowCount; //
     
    if (x != NULL) {
        *x = itemX;
    }
    if (y != NULL) {
        *y = itemY;
    }
}

// 根据偏移量计算item的origin
- (NSUInteger)originItemAtX:(NSUInteger)x
                          y:(NSUInteger)y {
    NSUInteger item = x * self.rowCount + y;
    return item;
}

- (CGRect)rectAtItem:(NSInteger)item
{
    NSUInteger page = item/(self.itemCountPerRow * self.rowCount);
    
    // 将item专程在当前page内的位置itemInPage
    NSInteger itemInPage = item % (self.itemCountPerRow * self.rowCount);
    
    CGRect targetFrame = CGRectMake(0, 0, self.itemSize.width, self.itemSize.height);
   
    targetFrame.origin.x = (page * self.collectionView.width) + self.sectionInset.left + (itemInPage % self.itemCountPerRow) * (self.itemSize.width + self.minimumLineSpacing);
    targetFrame.origin.y = self.sectionInset.top + (itemInPage / self.itemCountPerRow) * (self.itemSize.height + self.minimumInteritemSpacing);
    
    return targetFrame;
}

#pragma mark - overwrite
@end
